package com.hotel.system.service;

import com.hotel.comm.bean.DubboReply;
import com.hotel.comm.bean.IReply;
import com.hotel.comm.bean.system.EjobCity;
import com.hotel.comm.bean.system.EjobIndustryClass;
import com.hotel.comm.service.system.ISystemService;
import com.hotel.system.repository.IEjobCityRepository;
import com.hotel.system.repository.IEjobIndustryClassRepository;
import com.hotel.system.repository.IEjobIndustryRepository;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@SuppressWarnings({"unchecked", "rawtypes"})
@Service
public class SystemServiceImpl implements ISystemService {
    private static final String CITY_AF = "ABCDEF";
    private static final String CITY_GJ = "GHIJ";
    private static final String CITY_KN = "KLMN";
    private static final String CITY_OR = "OPQR";
    private static final String CITY_SV = "STUV";
    private static final String CITY_WZ = "WXYZ";
    @Autowired
    private IEjobCityRepository ejobCityRepository;
    @Autowired
    private IEjobIndustryClassRepository ejobIndustryClassRepository;
    @Autowired
    private IEjobIndustryRepository ejobIndustryRepository;

    @Override
    public IReply findAllOnlineCity() {
        List<EjobCity> citys = ejobCityRepository.findAllOnlineCity();
        return new DubboReply(citys);
    }

    @Override
    public IReply findAllTopCitys() {
        List<EjobCity> topCitys = this.ejobCityRepository.findAllTopCitys();
        return new DubboReply(topCitys);
    }

    @Override
    public IReply findAllNonTopCitys() {
        List<EjobCity> nontopCitys = this.ejobCityRepository.findAllNonTopCitys();
        Map<String, List<EjobCity>> nontopCityMap = null;
        if (CollectionUtils.isNotEmpty(nontopCitys)) {
            for (EjobCity city : nontopCitys) {
                if (nontopCityMap == null) {
                    nontopCityMap = new TreeMap<String, List<EjobCity>>();
                }
                if (city.getCityNamePy().startsWith("a") || city.getCityNamePy().startsWith("b")
                        || city.getCityNamePy().startsWith("c") || city.getCityNamePy().startsWith("d")
                        || city.getCityNamePy().startsWith("e") || city.getCityNamePy().startsWith("f")) {
                    List<EjobCity> afCitys = nontopCityMap.get(CITY_AF);
                    if (afCitys == null) {
                        afCitys = new ArrayList<EjobCity>();
                        nontopCityMap.put(CITY_AF, afCitys);
                    }
                    afCitys.add(city);
                }
                if (city.getCityNamePy().startsWith("g") || city.getCityNamePy().startsWith("h")
                        || city.getCityNamePy().startsWith("i") || city.getCityNamePy().startsWith("j")) {
                    List<EjobCity> gjCitys = nontopCityMap.get(CITY_GJ);
                    if (gjCitys == null) {
                        gjCitys = new ArrayList<EjobCity>();
                        nontopCityMap.put(CITY_GJ, gjCitys);
                    }
                    gjCitys.add(city);
                }
                if (city.getCityNamePy().startsWith("k") || city.getCityNamePy().startsWith("l")
                        || city.getCityNamePy().startsWith("m") || city.getCityNamePy().startsWith("n")) {
                    List<EjobCity> knCitys = nontopCityMap.get(CITY_KN);
                    if (knCitys == null) {
                        knCitys = new ArrayList<EjobCity>();
                        nontopCityMap.put(CITY_KN, knCitys);
                    }
                    knCitys.add(city);
                }
                if (city.getCityNamePy().startsWith("o") || city.getCityNamePy().startsWith("p")
                        || city.getCityNamePy().startsWith("q") || city.getCityNamePy().startsWith("r")) {
                    List<EjobCity> orCitys = nontopCityMap.get(CITY_OR);
                    if (orCitys == null) {
                        orCitys = new ArrayList<EjobCity>();
                        nontopCityMap.put(CITY_OR, orCitys);
                    }
                    orCitys.add(city);
                }
                if (city.getCityNamePy().startsWith("s") || city.getCityNamePy().startsWith("t")
                        || city.getCityNamePy().startsWith("u") || city.getCityNamePy().startsWith("v")) {
                    List<EjobCity> svCitys = nontopCityMap.get(CITY_SV);
                    if (svCitys == null) {
                        svCitys = new ArrayList<EjobCity>();
                        nontopCityMap.put(CITY_SV, svCitys);
                    }
                    svCitys.add(city);
                }
                if (city.getCityNamePy().startsWith("w") || city.getCityNamePy().startsWith("x")
                        || city.getCityNamePy().startsWith("y") || city.getCityNamePy().startsWith("z")) {
                    List<EjobCity> wzCitys = nontopCityMap.get(CITY_WZ);
                    if (wzCitys == null) {
                        wzCitys = new ArrayList<EjobCity>();
                        nontopCityMap.put(CITY_WZ, wzCitys);
                    }
                    wzCitys.add(city);
                }
            }
        }
        return new DubboReply(nontopCityMap);
    }

    @Override
    public List<EjobIndustryClass> findAllIndustryClass() {
        return this.ejobIndustryClassRepository.findAll();
//		List<EjobIndustryClass> industryClasses = this.ejobIndustryClassRepository.findAll();
//		for(EjobIndustryClass industryClass : industryClasses){
//			List<EjobIndustry> industries = this.ejobIndustryRepository.findByIndustryClassId(industryClass.getId());
//			industryClass.setEjobIndustryList(industries);
//		}
//		return industryClasses;
    }

}
